package com.example.database.slice;

import com.example.database.ResourceTable;
import com.example.database.Utils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;

import java.io.*;

public class PreferenceAbilitySlice extends AbilitySlice implements Component.ClickedListener {

    private Button mBtnPreferencePut;
    private Button mBtnPreferenceGet;
    private Button mBtnPreferenceDelete;
    private Button mBtnPreferenceCountPlus1;
    private Text mTextPreferenceCount;

    private Preferences.PreferencesObserver observer = new Preferences.PreferencesObserver() {
        @Override
        public void onChange(Preferences preferences, String s) {
            if(s == "count") {
                // 获取Count值
                final int count = preferences.getInt(s, 0 );
                getUITaskDispatcher().asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        mTextPreferenceCount.setText("Count: " + count);
                    }
                });
            }
        }
    };

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_preference);

        mBtnPreferencePut = (Button) findComponentById(ResourceTable.Id_btn_preference_put);
        mBtnPreferencePut.setClickedListener(this);
        mBtnPreferenceGet = (Button) findComponentById(ResourceTable.Id_btn_preference_get);
        mBtnPreferenceGet.setClickedListener(this);
        mBtnPreferenceDelete = (Button) findComponentById(ResourceTable.Id_btn_preference_delete);
        mBtnPreferenceDelete.setClickedListener(this);
        mBtnPreferenceCountPlus1 = (Button) findComponentById(ResourceTable.Id_btn_preference_count_plus1);
        mBtnPreferenceCountPlus1.setClickedListener(this);
        mTextPreferenceCount = (Text) findComponentById(ResourceTable.Id_text_preference_count);

        // 获取应用偏好数据库的存储位置
        Utils.log("getPreferencesDir: " + getPreferencesDir().toString());

        // 注册观察者对象
        getTestPreferences().registerObserver(observer);
    }

    @Override
    public void onClick(Component component) {

        if (component.getId() == ResourceTable.Id_btn_preference_put) {

            // 获取Preferences对象
            Preferences preferences = getTestPreferences();
            // 保存布尔型数据
            preferences.putBoolean("isAutoUpdate", true);
            // 保存整型数据
            preferences.putInt("version", 1001);
            // 保存字符串数据
            preferences.putString("currentUser", "Dong Yu");
            preferences.flush();


        }
        if (component.getId() == ResourceTable.Id_btn_preference_get) {

            // 获取Preferences对象
            Preferences preferences = getTestPreferences();
            // 获取布尔型数据
            boolean isAutoUpdate = preferences.getBoolean("isAutoUpdate", false);
            Utils.log("Is Auto Update: " + isAutoUpdate);
            // 获取整型数据
            int version = preferences.getInt("version", -1);
            Utils.log("Version: " + version);
            // 获取字符串数据
            String currentUser = preferences.getString("currentUser", "NO USER");
            Utils.log("Current User: " + currentUser);
        }

        if (component.getId() == ResourceTable.Id_btn_preference_delete) {

            // 获取Preferences对象
            Preferences preferences = getTestPreferences();
            // 删除键名为version的键值对
            preferences.delete("version");
        }

        if (component.getId() == ResourceTable.Id_btn_preference_count_plus1) {

            // 获取Preferences对象
            Preferences preferences = getTestPreferences();
            // 获取当前Count值
            int count = preferences.getInt("count", 0);
            // Count值加1，并保存数据库中
            preferences.putInt("count", count + 1);
            preferences.flush();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        // 取消注册观察者对象
        getTestPreferences().unregisterObserver(observer);
    }

    /**
     * 获取测试应用偏好数据库test的Preferences对象
     *
     * @return Preferences对象
     */
    private Preferences getTestPreferences() {
        // 获得DatabaseHelper对象
        DatabaseHelper databaseHelper = new DatabaseHelper(this);
        // 通过数据库名称获取Preferences
        Preferences preferences = databaseHelper.getPreferences("test");
        return preferences;
    }
}
